<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>连接设备</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f4f7fc;
        }
        .container {
            margin-top: 50px;
        }
        .card {
            border-radius: 15px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            background-color: white;
        }
        .table th, .table td {
            vertical-align: middle;
        }
        .btn-refresh {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 16px;
            border: none;
        }
        .btn-refresh:hover {
            background-color: #0056b3;
        }
        .table {
            border-radius: 10px;
        }
        .table-striped tbody tr:nth-child(odd) {
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="card">
        <h3 class="mb-4 text-center">已连接设备</h3>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>MAC 地址</th>
                    <th>IP 地址</th>
                    <th>主机名</th>
                    <th>上线时间</th>
                </tr>
            </thead>
            <tbody id="device-table-body">
                <tr><td colspan="4" class="text-center">加载中...</td></tr>
            </tbody>
        </table>
        <div class="d-flex justify-content-center mt-3">
            <button class="btn-refresh" onclick="fetchDevices()">刷新设备列表</button>
        </div>
    </div>
</div>

<script>
function fetchDevices() {
    fetch('/devices/json')
        .then(response => response.json())
        .then(data => {
            const tbody = document.getElementById('device-table-body');
            tbody.innerHTML = '';

            if (data.length === 0) {
                tbody.innerHTML = '<tr><td colspan="4" class="text-center">暂无连接设备。</td></tr>';
                return;
            }

            data.forEach(device => {
                const row = `
                    <tr>
                        <td>${device.mac}</td>
                        <td>${device.ip}</td>
                        <td>${device.hostname}</td>
                        <td>${device.time}</td>
                    </tr>
                `;
                tbody.innerHTML += row;
            });
        })
        .catch(error => {
            console.error('设备获取失败:', error);
            const tbody = document.getElementById('device-table-body');
            tbody.innerHTML = '<tr><td colspan="4" class="text-center text-danger">加载失败</td></tr>';
        });
}

// 初始加载
fetchDevices();

// 每5秒更新一次
setInterval(fetchDevices, 5000);
</script>

</body>
</html>
